package 面试经典150题.栈;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author tmh
 * @date 2024/6/14 22:39
 * @description
 */
public class T20有效的括号 {
    public boolean isValid(String s) {
        Deque<Character> deque = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if ('(' == c) {
                deque.push(')');
            } else if ('[' == c) {
                deque.push(']');
            } else if ('{' == c) {
                deque.push('}');
                //下面这段代码对应的就是 如果栈为空或者是栈顶元素不为c，那么就直接返回false
            } else if (deque.isEmpty() || deque.peek() != c) {
                return false;
            } else {
                deque.pop();
            }
        }
        return deque.isEmpty();
    }
}
